{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['.DS_Store', 'batches.meta', 'data_batch_1', 'data_batch_2', 'data_batch_3', 'data_batch_4', 'data_batch_5', 'readme.html', 'test_batch']\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf  #神经网络包\n",
    "import os     #os包也需要导入\n",
    "import cPickle #读cifardata包中 某标准格式图片格式\n",
    "import numpy as np #数值计算扩展 比如矩阵等\n",
    "#单个神经元，在2分类上，逻辑斯蒂模型的 解\n",
    "\n",
    "\n",
    "CIFAR_DIR= \"./cifar-10-batches-py\"  \n",
    "print os.listdir(CIFAR_DIR)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# data = cPickle.load(f) \n",
    "# data 格式   是dict ： 4个key  ['data','labels','batch_label','filenames']\n",
    "#             具体类型   numpy.ndarray , list   , str         ,   list\n",
    "#                    shape:(10000,3072) 数字标签，‘第几个训练集合文件’  此图片文件名\n",
    "#                       3072=32*32*3rgb通道\n",
    "#                      r+g+B 3部分数据拼接(每一部分都是 32*32个256内的数字)\n",
    "#      data['data'][0:2]  就是前2个list的数据\n",
    "# 后的对象， 可以data.keys() ,data['data']\n",
    "# 比如解析第101张图片的\n",
    "# image_arr = data['data'][101] 这是numpy里的类型\n",
    "# image_arr = image_arr.reshape((3,32,32))  类似于把数据重新展成3*32*32维度的\n",
    "# image_arr = image_arr.transpose((1,2,0)) # 因为我们要显示的数据格式应为 32*32*3 而不是源文件的格式，所以转换下\n",
    "#\n",
    "# 显示图片函数 import matplotlib.pyplot as plt\n",
    "#            from matplotlib.pyplot import imshow\n",
    "#           %matplotlib inline  在当前页面显示图片，而不是新建页面\n",
    "# imshow(image_arr)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10000, 3072)\n",
      "(10000,)\n",
      "(2000, 3072)\n",
      "(2000,)\n"
     ]
    }
   ],
   "source": [
    "def load_data(filename):\n",
    "        \"\"\"read data from data file.\"\"\"\n",
    "        with open(filename,'rb') as f:\n",
    "            data = cPickle.load(f) # f是打开的文件类型\n",
    "            return data['data'],data['labels'] \n",
    "        \n",
    "# tensorflow.Dataset 可以很方便地输入解析数据        \n",
    "class CifarData:\n",
    "    def __init__(self,filenames,need_shuffle):\n",
    "        all_data = []\n",
    "        all_labels = []\n",
    "        for filename in filenames:\n",
    "            data,labels = load_data(filename)\n",
    "            for item,label in zip(data,labels):\n",
    "                if label in [0,1]:\n",
    "                    all_data.append(item)\n",
    "                    all_labels.append(label)\n",
    "                \n",
    "        self._data = np.vstack(all_data)\n",
    "        #缩放图像  未做缩放归一化的时候，结果在50%左右，做了归一化后结果在80%以上了。因为不做归一化时可能出现结果偏向一方，导致梯度消失，导致错误发生\n",
    "        self._data = self._data / 127.5-1\n",
    "        self._labels = np.hstack(all_labels)\n",
    "        \n",
    "        print self._data.shape\n",
    "        print self._labels.shape\n",
    "        \n",
    "        self._num_examples = self._data.shape[0]\n",
    "        self._need_shuffle = need_shuffle\n",
    "        self._indicator = 0\n",
    "        if self._need_shuffle:\n",
    "                self._shuffle_data()\n",
    "           \n",
    "    def _shuffle_data(self):\n",
    "        #[0,1,2,3,4,5,]->[5,2,1,4,3,0]\n",
    "        p = np.random.permutation(self._num_examples)\n",
    "        self._data = self._data[p]\n",
    "        self._labels = self._labels[p]\n",
    "        \n",
    "    def next_batch(self,batch_size):\n",
    "        \"\"\"return batch_size examples as a batch.\"\"\"\n",
    "        end_indicator = self._indicator + batch_size\n",
    "        if end_indicator > self._num_examples:\n",
    "            if self._need_shuffle:\n",
    "                self._shuffle_data()\n",
    "                self._indicator = 0\n",
    "                end_indicator = batch_size\n",
    "            else:\n",
    "                raise Exception(\"have no more examples\")\n",
    "        if end_indicator > self._num_examples:\n",
    "            raise Exception(\"batch size is larger than examples\")\n",
    "        batch_data = self._data[self._indicator: end_indicator]\n",
    "        batch_labels = self._labels[self._indicator: end_indicator]\n",
    "        self._indicator = end_indicator\n",
    "        return batch_data, batch_labels\n",
    "            \n",
    "                \n",
    "            \n",
    "train_filenames= [os.path.join(CIFAR_DIR,'data_batch_%d' % i ) for i in range(1,6)]\n",
    "test_filenames = [os.path.join(CIFAR_DIR,'test_batch')]\n",
    "        \n",
    "train_data =CifarData(train_filenames,True)    \n",
    "test_data = CifarData(test_filenames,False)\n",
    "        \n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 重建图，不然错误后会一直报错\n",
    "tf.reset_default_graph()\n",
    "\n",
    "#下面开始构建神经网络图模型\n",
    "\n",
    "x = tf.placeholder(tf.float32, [None,3072])  #这是要存储data数据\n",
    "# [None]  之所以使用None，和minbatch，小规模输入训练都有关系，batch——size的可变性\n",
    "y = tf.placeholder(tf.int64, [None]) #这是要存储label值。 \n",
    "\n",
    "\n",
    "#以下是变量\n",
    "# (3072 * 1)   x中的每一个值，都需要有w与其做内积  x1*w1,x2*w2,....x3072*w3072\n",
    "#    x.get_shape()[-1] = x的纵列数=3072，     1 代表着输出是1个数字\n",
    "w = tf.get_variable('w', [x.get_shape()[-1], 1],\n",
    "                   initializer=tf.random_normal_initializer(0, 1))\n",
    "#                   初始化w，             使用正态分布均值0，方差1\n",
    "\n",
    "# (1,)        偏值，说这个值和w的第2维度有关系，也就是1个输出值，所以是1\n",
    "b = tf.get_variable('b', [1],\n",
    "                   initializer=tf.constant_initializer(0.0))\n",
    "\n",
    "# [None,3072] *[3072,1] = [None,1]\n",
    "y_ = tf.matmul(x, w) + b\n",
    "#     矩阵乘法\n",
    "\n",
    "#[None,1] 因为仅仅对y_进行sigmoid函数转换，所以还是原shape\n",
    "p_y_1 = tf.nn.sigmoid(y_)  #将y_值经过sigmoid函数变成概率值 ，类型是float32 【0，1】\n",
    "\n",
    "\n",
    "#用p_y_1和真实的y进行对比分析。\n",
    "\n",
    "y_reshaped = tf.reshape(y, (-1, 1)) #把y reshape成None -> None,1，这样就方便和p_y_1进行对比分析了\n",
    "#y_reshaped是 int64类型\n",
    "# y是int64的 ，p_y_1是 float32的\n",
    "y_reshaped_float = tf.cast(y_reshaped,tf.float32) #转换类型为float32\n",
    "\n",
    "#平方差计算 损失函数，计算p_y_1 和y_reshaped\n",
    "loss = tf.reduce_mean(tf.square(y_reshaped_float - p_y_1))# 让label类型值-各个数据值*权值矩阵后归一化为0~1\n",
    "#          平均值       平方\n",
    "\n",
    "# bool\n",
    "predict = p_y_1 > 0.5 #预测值，预测成为1或者0，是否算法输出的结果为偏向于1而不是0\n",
    "\n",
    "#[1,0,1,0,1,1,1,0,0,1,0]              0或者1            init64中的0-9种类型\n",
    "correct_prediction = tf.equal(tf.cast(predict,tf.int64),y_reshaped)# 如果结果是0或者1的类型\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float64))\n",
    "#           平均值    求上一行代码的平均值   correct_prediction  预测中的次数\n",
    " \n",
    "\n",
    "#定义梯度下降方法    相当于指定优化值，也就是损失函数\n",
    "with tf.name_scope('train_op'):\n",
    "    train_op = tf.train.AdamOptimizer(1e-3).minimize(loss) #tenforflow函数，梯度下降的变种\n",
    "#这一步也是 tensorflow 构建的\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Train] Step: 500, loss: 0.29724, acc: 0.70000\n",
      "[Train] Step: 1000, loss: 0.24877, acc: 0.75000\n",
      "[Train] Step: 1500, loss: 0.29731, acc: 0.70000\n",
      "[Train] Step: 2000, loss: 0.14805, acc: 0.85000\n",
      "[Train] Step: 2500, loss: 0.38815, acc: 0.60000\n",
      "[Train] Step: 3000, loss: 0.20000, acc: 0.80000\n",
      "[Train] Step: 3500, loss: 0.15001, acc: 0.85000\n",
      "[Train] Step: 4000, loss: 0.20929, acc: 0.80000\n",
      "[Train] Step: 4500, loss: 0.14901, acc: 0.85000\n",
      "[Train] Step: 5000, loss: 0.12389, acc: 0.85000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 5000, acc: 0.80250\n",
      "[Train] Step: 5500, loss: 0.13247, acc: 0.85000\n",
      "[Train] Step: 6000, loss: 0.40522, acc: 0.55000\n",
      "[Train] Step: 6500, loss: 0.15000, acc: 0.85000\n",
      "[Train] Step: 7000, loss: 0.05230, acc: 0.95000\n",
      "[Train] Step: 7500, loss: 0.41121, acc: 0.55000\n",
      "[Train] Step: 8000, loss: 0.29643, acc: 0.70000\n",
      "[Train] Step: 8500, loss: 0.09165, acc: 0.90000\n",
      "[Train] Step: 9000, loss: 0.28384, acc: 0.70000\n",
      "[Train] Step: 9500, loss: 0.20179, acc: 0.80000\n",
      "[Train] Step: 10000, loss: 0.29976, acc: 0.70000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 10000, acc: 0.80900\n",
      "[Train] Step: 10500, loss: 0.15247, acc: 0.85000\n",
      "[Train] Step: 11000, loss: 0.09994, acc: 0.90000\n",
      "[Train] Step: 11500, loss: 0.20667, acc: 0.80000\n",
      "[Train] Step: 12000, loss: 0.05665, acc: 0.95000\n",
      "[Train] Step: 12500, loss: 0.19903, acc: 0.80000\n",
      "[Train] Step: 13000, loss: 0.14127, acc: 0.85000\n",
      "[Train] Step: 13500, loss: 0.15000, acc: 0.85000\n",
      "[Train] Step: 14000, loss: 0.21337, acc: 0.75000\n",
      "[Train] Step: 14500, loss: 0.15000, acc: 0.85000\n",
      "[Train] Step: 15000, loss: 0.24335, acc: 0.75000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 15000, acc: 0.81050\n",
      "[Train] Step: 15500, loss: 0.00004, acc: 1.00000\n",
      "[Train] Step: 16000, loss: 0.15090, acc: 0.85000\n",
      "[Train] Step: 16500, loss: 0.25000, acc: 0.75000\n",
      "[Train] Step: 17000, loss: 0.23645, acc: 0.75000\n",
      "[Train] Step: 17500, loss: 0.18071, acc: 0.80000\n",
      "[Train] Step: 18000, loss: 0.15345, acc: 0.85000\n",
      "[Train] Step: 18500, loss: 0.19853, acc: 0.80000\n",
      "[Train] Step: 19000, loss: 0.15625, acc: 0.85000\n",
      "[Train] Step: 19500, loss: 0.00013, acc: 1.00000\n",
      "[Train] Step: 20000, loss: 0.00260, acc: 1.00000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 20000, acc: 0.81300\n",
      "[Train] Step: 20500, loss: 0.14947, acc: 0.85000\n",
      "[Train] Step: 21000, loss: 0.12861, acc: 0.85000\n",
      "[Train] Step: 21500, loss: 0.10004, acc: 0.90000\n",
      "[Train] Step: 22000, loss: 0.15000, acc: 0.85000\n",
      "[Train] Step: 22500, loss: 0.10000, acc: 0.90000\n",
      "[Train] Step: 23000, loss: 0.00827, acc: 1.00000\n",
      "[Train] Step: 23500, loss: 0.15061, acc: 0.85000\n",
      "[Train] Step: 24000, loss: 0.00000, acc: 1.00000\n",
      "[Train] Step: 24500, loss: 0.21114, acc: 0.80000\n",
      "[Train] Step: 25000, loss: 0.15000, acc: 0.85000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 25000, acc: 0.81050\n",
      "[Train] Step: 25500, loss: 0.19829, acc: 0.80000\n",
      "[Train] Step: 26000, loss: 0.40896, acc: 0.60000\n",
      "[Train] Step: 26500, loss: 0.22085, acc: 0.75000\n",
      "[Train] Step: 27000, loss: 0.01508, acc: 0.95000\n",
      "[Train] Step: 27500, loss: 0.10389, acc: 0.90000\n",
      "[Train] Step: 28000, loss: 0.10000, acc: 0.90000\n",
      "[Train] Step: 28500, loss: 0.09997, acc: 0.90000\n",
      "[Train] Step: 29000, loss: 0.10899, acc: 0.90000\n",
      "[Train] Step: 29500, loss: 0.25689, acc: 0.70000\n",
      "[Train] Step: 30000, loss: 0.24648, acc: 0.75000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 30000, acc: 0.81650\n",
      "[Train] Step: 30500, loss: 0.19923, acc: 0.80000\n",
      "[Train] Step: 31000, loss: 0.00058, acc: 1.00000\n",
      "[Train] Step: 31500, loss: 0.10266, acc: 0.90000\n",
      "[Train] Step: 32000, loss: 0.14995, acc: 0.85000\n",
      "[Train] Step: 32500, loss: 0.23836, acc: 0.75000\n",
      "[Train] Step: 33000, loss: 0.25000, acc: 0.75000\n",
      "[Train] Step: 33500, loss: 0.16097, acc: 0.85000\n",
      "[Train] Step: 34000, loss: 0.05524, acc: 0.95000\n",
      "[Train] Step: 34500, loss: 0.20007, acc: 0.80000\n",
      "[Train] Step: 35000, loss: 0.10000, acc: 0.90000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 35000, acc: 0.81750\n",
      "[Train] Step: 35500, loss: 0.10000, acc: 0.90000\n",
      "[Train] Step: 36000, loss: 0.25485, acc: 0.75000\n",
      "[Train] Step: 36500, loss: 0.20221, acc: 0.80000\n",
      "[Train] Step: 37000, loss: 0.24936, acc: 0.75000\n",
      "[Train] Step: 37500, loss: 0.21823, acc: 0.75000\n",
      "[Train] Step: 38000, loss: 0.14938, acc: 0.85000\n",
      "[Train] Step: 38500, loss: 0.05023, acc: 0.95000\n",
      "[Train] Step: 39000, loss: 0.25061, acc: 0.75000\n",
      "[Train] Step: 39500, loss: 0.20000, acc: 0.80000\n",
      "[Train] Step: 40000, loss: 0.18723, acc: 0.80000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 40000, acc: 0.81450\n",
      "[Train] Step: 40500, loss: 0.30000, acc: 0.70000\n",
      "[Train] Step: 41000, loss: 0.00026, acc: 1.00000\n",
      "[Train] Step: 41500, loss: 0.20000, acc: 0.80000\n",
      "[Train] Step: 42000, loss: 0.10000, acc: 0.90000\n",
      "[Train] Step: 42500, loss: 0.10000, acc: 0.90000\n",
      "[Train] Step: 43000, loss: 0.25891, acc: 0.75000\n",
      "[Train] Step: 43500, loss: 0.32817, acc: 0.65000\n",
      "[Train] Step: 44000, loss: 0.10935, acc: 0.90000\n",
      "[Train] Step: 44500, loss: 0.10021, acc: 0.90000\n",
      "[Train] Step: 45000, loss: 0.05049, acc: 0.95000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 45000, acc: 0.82200\n",
      "[Train] Step: 45500, loss: 0.15010, acc: 0.85000\n",
      "[Train] Step: 46000, loss: 0.06201, acc: 0.95000\n",
      "[Train] Step: 46500, loss: 0.25067, acc: 0.75000\n",
      "[Train] Step: 47000, loss: 0.05080, acc: 0.95000\n",
      "[Train] Step: 47500, loss: 0.00019, acc: 1.00000\n",
      "[Train] Step: 48000, loss: 0.15000, acc: 0.85000\n",
      "[Train] Step: 48500, loss: 0.05000, acc: 0.95000\n",
      "[Train] Step: 49000, loss: 0.20215, acc: 0.80000\n",
      "[Train] Step: 49500, loss: 0.10317, acc: 0.90000\n",
      "[Train] Step: 50000, loss: 0.05037, acc: 0.95000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 50000, acc: 0.81950\n",
      "[Train] Step: 50500, loss: 0.22750, acc: 0.75000\n",
      "[Train] Step: 51000, loss: 0.00068, acc: 1.00000\n",
      "[Train] Step: 51500, loss: 0.24998, acc: 0.75000\n",
      "[Train] Step: 52000, loss: 0.25000, acc: 0.75000\n",
      "[Train] Step: 52500, loss: 0.13728, acc: 0.80000\n",
      "[Train] Step: 53000, loss: 0.15002, acc: 0.85000\n",
      "[Train] Step: 53500, loss: 0.20080, acc: 0.80000\n",
      "[Train] Step: 54000, loss: 0.35000, acc: 0.65000\n",
      "[Train] Step: 54500, loss: 0.10000, acc: 0.90000\n",
      "[Train] Step: 55000, loss: 0.09974, acc: 0.90000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 55000, acc: 0.81900\n",
      "[Train] Step: 55500, loss: 0.25008, acc: 0.75000\n",
      "[Train] Step: 56000, loss: 0.10102, acc: 0.90000\n",
      "[Train] Step: 56500, loss: 0.09853, acc: 0.90000\n",
      "[Train] Step: 57000, loss: 0.06372, acc: 0.95000\n",
      "[Train] Step: 57500, loss: 0.15415, acc: 0.85000\n",
      "[Train] Step: 58000, loss: 0.20005, acc: 0.80000\n",
      "[Train] Step: 58500, loss: 0.10001, acc: 0.90000\n",
      "[Train] Step: 59000, loss: 0.05710, acc: 0.95000\n",
      "[Train] Step: 59500, loss: 0.20027, acc: 0.80000\n",
      "[Train] Step: 60000, loss: 0.05108, acc: 0.95000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 60000, acc: 0.81900\n",
      "[Train] Step: 60500, loss: 0.14988, acc: 0.85000\n",
      "[Train] Step: 61000, loss: 0.00000, acc: 1.00000\n",
      "[Train] Step: 61500, loss: 0.14770, acc: 0.85000\n",
      "[Train] Step: 62000, loss: 0.10006, acc: 0.90000\n",
      "[Train] Step: 62500, loss: 0.11442, acc: 0.85000\n",
      "[Train] Step: 63000, loss: 0.10260, acc: 0.90000\n",
      "[Train] Step: 63500, loss: 0.25802, acc: 0.75000\n",
      "[Train] Step: 64000, loss: 0.20515, acc: 0.80000\n",
      "[Train] Step: 64500, loss: 0.05008, acc: 0.95000\n",
      "[Train] Step: 65000, loss: 0.00000, acc: 1.00000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 65000, acc: 0.82100\n",
      "[Train] Step: 65500, loss: 0.05002, acc: 0.95000\n",
      "[Train] Step: 66000, loss: 0.10289, acc: 0.90000\n",
      "[Train] Step: 66500, loss: 0.10464, acc: 0.90000\n",
      "[Train] Step: 67000, loss: 0.09883, acc: 0.90000\n",
      "[Train] Step: 67500, loss: 0.05034, acc: 0.95000\n",
      "[Train] Step: 68000, loss: 0.05021, acc: 0.95000\n",
      "[Train] Step: 68500, loss: 0.16328, acc: 0.80000\n",
      "[Train] Step: 69000, loss: 0.10450, acc: 0.90000\n",
      "[Train] Step: 69500, loss: 0.14639, acc: 0.85000\n",
      "[Train] Step: 70000, loss: 0.00109, acc: 1.00000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 70000, acc: 0.81800\n",
      "[Train] Step: 70500, loss: 0.10262, acc: 0.90000\n",
      "[Train] Step: 71000, loss: 0.00000, acc: 1.00000\n",
      "[Train] Step: 71500, loss: 0.24757, acc: 0.75000\n",
      "[Train] Step: 72000, loss: 0.15025, acc: 0.85000\n",
      "[Train] Step: 72500, loss: 0.20777, acc: 0.80000\n",
      "[Train] Step: 73000, loss: 0.00338, acc: 1.00000\n",
      "[Train] Step: 73500, loss: 0.05124, acc: 0.95000\n",
      "[Train] Step: 74000, loss: 0.10303, acc: 0.90000\n",
      "[Train] Step: 74500, loss: 0.05003, acc: 0.95000\n",
      "[Train] Step: 75000, loss: 0.14637, acc: 0.85000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 75000, acc: 0.82050\n",
      "[Train] Step: 75500, loss: 0.05046, acc: 0.95000\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Train] Step: 76000, loss: 0.15111, acc: 0.85000\n",
      "[Train] Step: 76500, loss: 0.10086, acc: 0.90000\n",
      "[Train] Step: 77000, loss: 0.05988, acc: 0.95000\n",
      "[Train] Step: 77500, loss: 0.15017, acc: 0.85000\n",
      "[Train] Step: 78000, loss: 0.10580, acc: 0.90000\n",
      "[Train] Step: 78500, loss: 0.10000, acc: 0.90000\n",
      "[Train] Step: 79000, loss: 0.15044, acc: 0.85000\n",
      "[Train] Step: 79500, loss: 0.15000, acc: 0.85000\n",
      "[Train] Step: 80000, loss: 0.10004, acc: 0.90000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 80000, acc: 0.81650\n",
      "[Train] Step: 80500, loss: 0.20000, acc: 0.80000\n",
      "[Train] Step: 81000, loss: 0.10143, acc: 0.90000\n",
      "[Train] Step: 81500, loss: 0.05007, acc: 0.95000\n",
      "[Train] Step: 82000, loss: 0.10125, acc: 0.90000\n",
      "[Train] Step: 82500, loss: 0.00011, acc: 1.00000\n",
      "[Train] Step: 83000, loss: 0.14704, acc: 0.85000\n",
      "[Train] Step: 83500, loss: 0.15231, acc: 0.85000\n",
      "[Train] Step: 84000, loss: 0.05104, acc: 0.95000\n",
      "[Train] Step: 84500, loss: 0.15005, acc: 0.85000\n",
      "[Train] Step: 85000, loss: 0.05132, acc: 0.95000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 85000, acc: 0.81700\n",
      "[Train] Step: 85500, loss: 0.19999, acc: 0.80000\n",
      "[Train] Step: 86000, loss: 0.15134, acc: 0.85000\n",
      "[Train] Step: 86500, loss: 0.25599, acc: 0.75000\n",
      "[Train] Step: 87000, loss: 0.10002, acc: 0.90000\n",
      "[Train] Step: 87500, loss: 0.10000, acc: 0.90000\n",
      "[Train] Step: 88000, loss: 0.20005, acc: 0.80000\n",
      "[Train] Step: 88500, loss: 0.00036, acc: 1.00000\n",
      "[Train] Step: 89000, loss: 0.15051, acc: 0.85000\n",
      "[Train] Step: 89500, loss: 0.16571, acc: 0.80000\n",
      "[Train] Step: 90000, loss: 0.05020, acc: 0.95000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 90000, acc: 0.81650\n",
      "[Train] Step: 90500, loss: 0.15002, acc: 0.85000\n",
      "[Train] Step: 91000, loss: 0.00480, acc: 1.00000\n",
      "[Train] Step: 91500, loss: 0.25002, acc: 0.75000\n",
      "[Train] Step: 92000, loss: 0.19983, acc: 0.80000\n",
      "[Train] Step: 92500, loss: 0.10003, acc: 0.90000\n",
      "[Train] Step: 93000, loss: 0.20005, acc: 0.80000\n",
      "[Train] Step: 93500, loss: 0.25001, acc: 0.75000\n",
      "[Train] Step: 94000, loss: 0.09978, acc: 0.90000\n",
      "[Train] Step: 94500, loss: 0.00028, acc: 1.00000\n",
      "[Train] Step: 95000, loss: 0.15351, acc: 0.85000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 95000, acc: 0.81500\n",
      "[Train] Step: 95500, loss: 0.20031, acc: 0.80000\n",
      "[Train] Step: 96000, loss: 0.05951, acc: 0.95000\n",
      "[Train] Step: 96500, loss: 0.20430, acc: 0.80000\n",
      "[Train] Step: 97000, loss: 0.20046, acc: 0.80000\n",
      "[Train] Step: 97500, loss: 0.10243, acc: 0.90000\n",
      "[Train] Step: 98000, loss: 0.15159, acc: 0.85000\n",
      "[Train] Step: 98500, loss: 0.05138, acc: 0.95000\n",
      "[Train] Step: 99000, loss: 0.10077, acc: 0.90000\n",
      "[Train] Step: 99500, loss: 0.30071, acc: 0.70000\n",
      "[Train] Step: 100000, loss: 0.20000, acc: 0.80000\n",
      "(2000, 3072)\n",
      "(2000,)\n",
      "[Test ] Step: 100000, acc: 0.81000\n"
     ]
    }
   ],
   "source": [
    "init = tf.global_variables_initializer()\n",
    "batch_size = 20\n",
    "train_steps = 100000\n",
    "test_steps = 100\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for i in range(train_steps):\n",
    "        batch_data, batch_labels = train_data.next_batch(batch_size)\n",
    "        loss_val, acc_val, _ =  sess.run(\n",
    "            [loss, accuracy, train_op],\n",
    "            feed_dict={\n",
    "                 x: batch_data,\n",
    "                 y: batch_labels})\n",
    "        if (i+1) % 500 ==0:\n",
    "            print '[Train] Step: %d, loss: %4.5f, acc: %4.5f' \\\n",
    "                % (i+1, loss_val,acc_val)\n",
    "        if (i+1) % 5000 == 0:\n",
    "            test_data = CifarData(test_filenames,False)\n",
    "            all_test_acc_val = []\n",
    "            for j in range(test_steps):\n",
    "                test_batch_data, test_batch_labels  \\\n",
    "                    = test_data.next_batch(batch_size)\n",
    "                test_acc_val = sess.run(\n",
    "                    [accuracy],\n",
    "                    feed_dict = {\n",
    "                        x:test_batch_data,\n",
    "                        y:test_batch_labels\n",
    "                    })\n",
    "            \n",
    "                all_test_acc_val.append(test_acc_val)\n",
    "            test_acc = np.mean(all_test_acc_val)\n",
    "            print '[Test ] Step: %d, acc: %4.5f' % (i+1, test_acc)\n",
    "                \n",
    "            \n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
